var gFormData = null;
$(function () {
    $.getSecondType = function (list) {
        var secondType = [], length = list.length;
        for (var i = 0; i < length; i++) {
            secondType.push(list.eq(i).val());
        }
        console.log(secondType.join(","))
        return secondType.join(",");
    }
    gMainDT = $("#mainDataTable").DataTable({
        ajax: {
            url: "../rds_count/getCount4Type",
            type: 'post',
            data: function (d) {
                d.search = JSON.stringify(gFormData);
            },
        },
        columns: [{
            data: null
        }, {
            data: "province"
        }, {
            data: "city"
        }, {
            data: "area"
        }, {
            data: "roadName"
        }, {
            data: "startRoad"
        }, {
            data: "endRoad"
        }, {
            data: "count"
        }],
        processing: true,
        serverSide: true,
        ordering: false,
        select: false,
        autoFill: true,
        displayLength: 20,
        lengthMenu: [[5, 10, 15, 20, 50, 100, 150, -1], [5, 10, 15, 20, 50, 100, 150, "All"]],
        language: {
            url: "zh_cn.json"
        },
        dom: "<'row'<'col-md-4'l><'col-md-8'>r>t<'row'<'col-md-6'i><'col-md-6'p>>",
        fnDrawCallback: function () {// 序号自增回调函数
            var api = this.api();
            var startIndex = api.context[0]._iDisplayStart;
            api.column(0).nodes().each(function (cell, i) {
                cell.innerHTML = startIndex + i + 1;
            });
        }
    })

    $("#provinceChoose").select2();
    $("#cityChoose").select2();
    $("#areaChoose").select2();
    $("#roadNameChoose").select2();
    $("#startRoadChoose").select2();
    $("#endRoadChoose").select2();
    $("#orgNameChoose").select2();
    $("#mainOrgNameChoose").select2();
    $("#typeSelect").on("click", "input", function () {
        switch ($(this).val()) {
            case '0':
                $.showSecondType(0);
                break;
            case '1':
                $.showSecondType(1);
                break;
            case '2':
                $.showSecondType(2);
                break;
            case '3':
                $.showSecondType(3);
                break;
            case '4':
                $.showSecondType(4);
                break;
            case '6':
                $.showSecondType(6);
                break;
            case '7':
                $.showSecondType(7);
                break;
            case '8':
                $.showSecondType(8);
                break;
            case '9':
                $.showSecondType(9);
                break;
        }
    });

    $.showSecondType = function (n) {

        $("#secondSelect").children('div').hide();
        $("#secondSelect").children('div').eq(n).fadeIn();
        $.getPosition(n);
        $("#otherInput").fadeIn();
        $("#positionSelect").fadeIn();
    }

    $.getPosition = function (n) {
        $.ajax({
            url: "../rds_count/getPositionList",
            type: "post",
            data: {
                orgId: gUserLoginResult.orgId,
                orgType: gUserLoginResult.orgType,
                EquipmentType: n,
                actorIds: gUserLoginResult.userActors
            },
            success: function (msg) {
                if (msg) {
                    if (msg.isSuccess) {
                        var positionList = msg.jsonData;
                        $.cleanOption();
                        for (var i in positionList) {
                            console.log("name: " + i);
                            if (i.indexOf("rg") < 0)
                                positionList[i].forEach(function (t) {
                                    $("#" + i + "Choose").append("<option value='" + t + "'>" + t + "</option>");
                                });
                            else {
                                if (i.indexOf("main") >= 0) {
                                    positionList[i].forEach(function (t) {
                                        $("#mainOrgNameChoose").append("<option value='" + t.orgId + "'>" + t.orgName + "</option>")
                                    })
                                } else {
                                    positionList[i].forEach(function (t) {
                                        $("#orgNameChoose").append("<option value='" + t.orgId + "'>" + t.orgName + "</option>")
                                    })
                                }

                            }
                        }
                    } else
                        $.alert("获取区域列表失败! 请联系管理员", 2)
                }
            }

        })


    }

    $("#timeLimit").focus(function () {
        $('#timeLimit').daterangepicker({
            autoUpdateInput: false,
            locale: {
                format: 'YYYY-MM-DD',
                applyLabel: '确认',
                cancelLabel: '取消',
                fromLabel: '从',
                toLabel: '到',
                weekLabel: 'W',
                customRangeLabel: 'Custom Range',
                daysOfWeek: ["日", "一", "二", "三", "四", "五", "六"],
                monthNames: ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"],
            }
        }).on('cancel.daterangepicker', function (ev, picker) {
            $(this).val('');
        }).on('apply.daterangepicker', function (ev, picker) {
            $(this).val(picker.startDate.format('YYYY-MM-DD') + ' - ' + picker.endDate.format('YYYY-MM-DD'));
        });
    });

    $.cleanOption = function () {
        $("#provinceChoose").html("<option value=\"-1\">省份</option>");
        $("#cityChoose").html("<option value=\"-1\">市</option>");
        $("#areaChoose").html("<option value=\"-1\">区域</option>");
        $("#roadNameChoose").html("<option value=\"-1\">道路</option>");
        $("#startRoadChoose").html("<option value=\"-1\">起</option>");
        $("#endRoadChoose").html("<option value=\"-1\">止</option>");
        if (gUserLoginResult.userActors.indexOf("1") >= 0)
            $("#orgNameChoose").html("<option value=\"-1\">生产单位</option>");
        else
            $("#orgNameChoose").html("");
        $("#mainOrgNameChoose").html("<option value=\"-1\">监管单位</option>");

    }

    $("#searchRoom").on("click", "#searchBtn", function () {
        if ($("#secondSelect input:visible:checked").length == 0) {
            $.alert("请选择具体分类！", 2);
            return;
        }
        var type = $("#typeSelect input:checked").val();
        if (type == 2 || type == 3)
            gMainDT.column(4).visible(false);
        else
            gMainDT.column(4).visible(true);
        gFormData = {
            type: type,
            secondType: $.getSecondType($("#secondSelect input:visible:checked")),
            province: $("#provinceChoose").val(),
            city: $("#cityChoose").val(),
            area: $("#areaChoose").val(),
            roadName: $("#roadNameChoose").val(),
            startRoad: $("#startRoadChoose").val(),
            endRoad: $("#endRoadChoose").val(),
            orgName: $("#orgNameChoose").val(),
            mainOrgName: $("#mainOrgNameChoose").val(),
            projectNumber: $("#projectNumber").val(),
            timeLimit: $("#timeLimit").val()
        };
        gMainDT.search(gFormData).draw();
    })

});
